const EventEmitter = require('events');
class Queue extends Set {
  constructor() {
    super();
    const events = this.events = new EventEmitter();
    Object.keys(EventEmitter.prototype).forEach(prop_name => {
      if (events[prop_name] instanceof Function) {
        this[prop_name] = (...args) => events[prop_name](...args)
      }
    })
  }
  add(...args) {
    const res = super.add(...args)
    this.events.emit("sizeChanged", this.size);
    return res;
  }
  delete(...args) {
    const res = super.delete(...args)
    this.events.emit("sizeChanged", this.size);
    return res;
  }
  clear(...args) {
    const old_size = this.size;
    const res = super.clear(...args)
    old_size !== 0 && this.events.emit("sizeChanged", this.size);
    return res;
  }
}
module.exports = Queue;
